function show(ele) {
    ele.style.display = 'block'
}
function hide(ele) {
    ele.style.display = 'none'
}


var scrolltop
let tbhFixedtool = document.querySelector('.tbh-fixedtool')
let scrollBtnList = tbhFixedtool.querySelectorAll('a')
let targetDivList = document.querySelectorAll('.scrollto')
let targetYList = []

window.onscroll = function () {
    scrolltop = document.body.scrollTop || document.documentElement.scrollTop
    let fixedBar = document.querySelector(".top").parentNode
    // console.log(scrolltop);
    //获取侧边栏
    let fixedtool = document.querySelector('.tbh-fixedtool')

    //显示导航
    if (scrolltop >= 120) {
        fixedBar.classList.add("wrap-fixed")
        let searchTriggers = fixedBar.querySelector('.search-triggers')
        searchTriggers.onmouseenter = function () {
            this.classList.add('triggers-hover')
        }
        searchTriggers.onmouseleave = function () {
            this.classList.remove('triggers-hover')
        }
    } else {
        fixedBar.classList.remove("wrap-fixed")
    }

    //侧边工具栏
    //滚动部分大于侧边工具栏距离顶部距离，侧边工具栏固定定位
    if (scrolltop >= 120 && scrolltop < 422) {
        fixedtool.style.position = 'absolute'
        fixedtool.style.top = '487px'
    } else if (scrolltop >= 422) {
        fixedtool.style.position = 'fixed'
        fixedtool.style.top = '75px'
    } else {
        fixedtool.style.position = 'absolute'
        fixedtool.style.top = '478px'
    }

    // 显示固定侧边栏顶部按钮
    if (scrolltop > 1000) {
        scrollBtnList[5].style.display = 'block'
    } else {
        scrollBtnList[5].style.display = 'none'
    }
}

window.onload = function () {
    scrolltop = document.body.scrollTop || document.documentElement.scrollTop
    console.log(scrolltop)
    document.body.scrollTop = document.documentElement.scrollTop = 0
}

// 点击侧边栏滚动封装
for (let i = 0; i < 6; i++) {
    if (i == 5) {
        targetYList[i] = 0
    } else {
        targetYList[i] = targetDivList[i].offsetTop - 54
    }
    let num = i
    scrollBtnList[i].onclick = function (e) {
        e.preventDefault()
        scrollAnimate(targetYList[num])
        for (let i = 0; i < scrollBtnList.length; i++) {
            scrollBtnList[i].classList.remove('on')
        }
        scrollBtnList[i].classList.add('on')
    }
}

// 滚动动画封装
let timer = null
function scrollAnimate(targetY) {
    let nowY = scrolltop || 0
    let step = (targetY - nowY) / 25
    clearInterval(timer)
    timer = setInterval(function () {
        if (Math.abs(nowY - targetY) < Math.abs(step)) {
            window.scrollTo(0, targetY)
            clearInterval(timer)
        } else {
            nowY += step
            window.scrollTo(0, nowY)
        }
    }, 10)
}

//搜索部分事件
{
    //关闭二维码
    let top = document.querySelector('.top')
    let qr = top.querySelector('.qr')
    let closeQrBtn = qr.querySelectorAll('span')[1]
    closeQrBtn.onclick = function () {
        qr.style.display = 'none'
    }

    //点击天猫店铺宝贝切换
    let searchTopUl = top.querySelector('.search_top')
    let searchTopLi = searchTopUl.querySelectorAll('li')
    let iconCamera = document.querySelector('.icon_camera')

    searchTopLi.forEach(function (item, i) {
        item.addEventListener('click', function () {
            if (i != 0) {
                iconCamera.style.display = 'none'
            }
            for (let i = 0; i < searchTopLi.length; i++) {
                searchTopLi[i].classList.remove('active')
            }
            this.classList.add('active')
            searchTopUl.insertBefore(this, searchTopUl.firstChild)
        })
    })
}

//主题市场
{
    let mainMenu = document.querySelector('.main_menu')
    let liArr = mainMenu.querySelectorAll('li')
    let serviceFloat = mainMenu.querySelector('.service-float')
    for (let i = 0; i < liArr.length; i++) {
        liArr[i].onmouseenter = function () {
            for (let i = 0; i < liArr.length; i++) {
                liArr[i].classList.remove('on')
            }
            this.classList.add('on')
            serviceFloat.style.display = 'block'
        }
        liArr[i].onmouseleave = function () {
            this.classList.remove('on')
            serviceFloat.style.display = 'none'
        }
    }
}

//轮播
{
    let swiperContainer = document.querySelectorAll('.swiper-container')

    for (let i = 0; i < swiperContainer.length; i++) {
        swiperContainer[i].onmouseenter = function () {
            let swiperBtn = this.querySelector('.swiper-button')
            show(swiperBtn)
        }
        swiperContainer[i].onmouseleave = function () {
            let swiperBtn = this.querySelector('.swiper-button')
            hide(swiperBtn)
        }
    }
}


{
    let siteNavMenu = document.querySelectorAll('.site-nav-menu')
    let siteNavBd = document.querySelectorAll('.site-nav-bd')
    
    for (let i = 0; i < siteNavMenu.length; i++) {
        siteNavMenu[i].onmouseenter = function () {
            console.log(this)
            this.classList.toggle('site-nav-menu')
            this.classList.toggle('site-nav-menu-hover')
        }
        siteNavMenu[i].onmouseleave = function () {
            this.classList.toggle('site-nav-menu')
            this.classList.toggle('site-nav-menu-hover')
        }
    }
}

// var footer = document.querySelector('.footer-ft')
// footer.innerHTML = '<style type="text/css">body{background-color: red}</style>'
window.onload = function () {
    var tbhFixedtool = document.querySelector('.tbh-fixedtool')
    tbhFixedtool.classList.add('fixed-show')
}